<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use App\Common\Utils\ShareViews;
use App\Http\Service\menus\MenusUserService;

class MenusMiddleware {

    public function handle($request, Closure $next) {
        //菜单存入缓存
        $cacheKey = md5(LEFT_DATAS_KEY . Auth::user()->id);
        $menusCacheDatas = Cache::get($cacheKey);
        if (!$menusCacheDatas) {
            $menusDatas = (new MenusUserService())->getUserMenus();
            Cache::put($cacheKey, serialize($menusDatas));
            $menusCacheDatas = Cache::get($cacheKey);
        }
        (new ShareViews())->setLeftView(unserialize($menusCacheDatas));
        return $next($request);
    }

}
